﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Web.Configuration;
using ElectronicCommerce.Framework.Web.Configuration.StringResource.List;

namespace ElectronicCommerce.Framework.Web.Validation
{
    public class ListHelper
    {
        public static List<ListItem> GetListById(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new List<ListItem>();
            }
            List<ListItem> temp = ConfigHelper.ListConfig.Groups[id].Items;
            if (temp.Count == 0)
            {
                return new List<ListItem>();
            }
            ListItem[] list = new ListItem[temp.Count];
            temp.CopyTo(list);
            return list.ToList<ListItem>();
        }

        public static bool IsTextExist(string text, string id)
        {
            if (string.IsNullOrEmpty(text))
            {
                return false;
            }
            List<ListItem> list = GetListById(id);
            foreach (ListItem item in list)
            {
                if (item.Text.Trim().Equals(text.Trim()))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsValueExist(string value, string id)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            List<ListItem> list = GetListById(id);
            foreach (ListItem item in list)
            {
                if (item.Value.Trim().Equals(value.Trim()))
                {
                    return true;
                }
            }
            return false;
        }

        public static string GetTextByValue(string value, string id)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            List<ListItem> list = GetListById(id);
            foreach (ListItem item in list)
            {
                if (item.Value.Trim().Equals(value.Trim()))
                {
                    return item.Text;
                }
            }
            return string.Empty;
        }

        public static string GetValueByText(string text, string id)
        {
            if (string.IsNullOrEmpty(text))
            {
                return string.Empty;
            }
            List<ListItem> list = GetListById(id);
            foreach (ListItem item in list)
            {
                if (item.Text.Trim().Equals(text.Trim()))
                {
                    return item.Value;
                }
            }
            return string.Empty;
        }
    }
}
